import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
]

export const asyncRoutes = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '系统首页', icon: 'dashboard', affix: true }
    }]
  },

  // 讲师管理
  {
    path: '/manage/teacher',
    component: Layout,
    redirect: '/manage/teacher/list',
    name: 'teacher',
    meta: { title: '讲师管理', icon: 'peoples' },
    children: [
      {
        path: 'list',
        name: 'teacherList',
        component: () => import('@/views/teacher/list'),
        meta: { title: '讲师列表' }
      },
      {
        path: 'edit/:id',
        name: 'teacherEdit',
        component: () => import('@/views/teacher/form'),
        meta: { title: '修改讲师', noCache: true },
        hidden: true
      },
      {
        path: 'audit',
        name: 'teacherAudit',
        component: () => import('@/views/audit/index'),
        meta: { title: '讲师审核列表' }
      },
    ]
  },

  //用户管理
  {
    path: '/manage/user',
    alwaysShow: true,
    component: Layout,
    redirect: '/manage/user/list',
    name: 'user',
    meta: { title: '用户管理', icon: 'user' },
    children: [
      {
        path: 'list',
        name: 'userList',
        component: () => import('@/views/user/list'),
        meta: { title: '用户列表' }
      },
    ]
  },

  //课程管理
  {
    path: '/manage/course',
    alwaysShow: true,
    component: Layout,
    redirect: '/manage/course/list',
    name: 'course',
    meta: { title: '课程管理', icon: 'education' },
    children: [
      {
        path: 'list',
        name: 'courseList',
        component: () => import('@/views/course/list'),
        meta: { title: '课程列表' }
      },
    ]
  },

  //首页管理
  {
    path: '/manage/ad',
    alwaysShow: true,
    component: Layout,
    redirect: '/manage/ad/list',
    name: 'ad',
    meta: { title: '首页管理', icon: 'component' },
    children: [
      {
        path: 'list',
        name: 'adList',
        component: () => import('@/views/ad/list'),
        meta: { title: '轮播图管理' }
      },
    ]
  },

  //订单管理
  {
    path: '/manage/order',
    alwaysShow: true,
    component: Layout,
    redirect: '/manage/order/list',
    name: 'order',
    meta: { title: '订单管理', icon: 'shopping' },
    children: [
      {
        path: 'list',
        name: 'orderList',
        component: () => import('@/views/order/list'),
        meta: { title: '订单列表' }
      },
    ]
  },

  //数据报表管理
  {
    path: '/manage/daily',
    alwaysShow: true,
    component: Layout,
    redirect: '/manage/daily/list',
    name: 'daily',
    meta: { title: '数据报表', icon: 'chart' },
    children: [
      {
        path: 'list',
        name: 'dailyList',
        component: () => import('@/views/daily/list'),
        meta: { title: '数据报表' }
      },
    ]
  },

  //课程分类管理
  {
    path: '/manage/category',
    component: Layout,
    redirect: '/manage/category/list',
    name: 'category',
    meta: { title : '课程分类管理', icon: 'nested'},
    children: [
      {
        path: 'list',
        name: 'categoryList',
        component: () => import('@/views/category/list'),
        meta: { title: '课程分类列表' }
      },
      {
        path: 'import',
        name: 'categoryImport',
        component: () => import('@/views/category/import'),
        meta: { title: '导入课程分类' }
      }
    ]
  },

  //问答管理
  {
    path: '/manage/question',
    component: Layout,
    alwaysShow: true,
    redirect: '/manage/question/list',
    name: 'question',
    meta: { title : '问答管理', icon: 'table'},
    children: [
      {
        path: 'list',
        name: 'questionList',
        component: () => import('@/views/question/list'),
        meta: { title: '问答列表' }
      },
      {
        path: 'tag',
        name: 'tagList',
        component: () => import('@/views/tag/list'),
        meta: { title: '问答标签列表' }
      },
      {
        path: 'edit/:id',
        name: 'questionEdit',
        component: () => import('@/views/question/edit'),
        meta: { title: '修改问答', noCache: true },
        hidden: true
      },
    ]
  },

  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
